﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;
using System.Drawing;
public partial class Manage_CompanyList : System.Web.UI.Page, HZ.MBSM.BLL.IPageDescribe
{
    public string strConditions;

    protected void Page_Load(object sender, EventArgs e)
    {
        string strSql;
        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {

            if (!IsPostBack)
            {
                Db DbProject = new Db();
                strSql = "SELECT AccountId,AccountName FROM ACCOUNT WHERE TYPE=0 OR TYPE=1 OR  TYPE=6  OR  TYPE=16 ";
                System.Data.IDataReader dr = DbProject.DataReader(strSql);
                lstSHR.DataSource = dr;
                lstSHR.Items.Add("全部");
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];

             
                while (dr.Read())
                {
                    lstSHR.Items.Add(dr["AccountName"].ToString());
                }
                lstSHR.Items.FindByValue(_Account.AccountName).Selected = true;
                dr.Close();
                dr.Dispose();
                DbProject.Close();
                lstQYType.Items.Add("全部");
                lstQYType.Items.Add("经营企业");
                lstQYType.Items.Add("生产企业");
                lstQYType.Items.Add("一级代理商");
               
                lstCheckStatus.Items.Add("全部");
                lstCheckStatus.Items.Add("未审核");
                lstCheckStatus.Items.Add("审核通过");
                lstCheckStatus.Items.Add("审核未通过");
                lstCheckStatus.Items.Add("资料一致");
                lstCheckStatus.SelectedIndex = 1;
                lstMemo2.Items.Add("全部");
                lstMemo2.Items.Add("已保存未提交");
                lstMemo2.Items.Add("已提交");
                lstMemo2.Items.Add("已修改");
                lstMemo2.SelectedIndex = 2;

            }
            if (Request.Form["btnSubmit"] == "删除" && Request.Form["chk"] != null)
            {
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
                if (_Account != null)
                {
                    if (_Account.Type == "0")
                    {

                        int rowCun = 0;
                        Db db = new Db();
                        string delAccountId = Request.Form["chk"].ToString();
                        strSql = "select count(accountid) as cun from company where (accountid in (select distinct accountid_sc from goods) or accountid in (select distinct accountid_agent from goods) or accountid in (select distinct accountid from project_bid) ) and  accountid in (" + delAccountId + ")";
                        System.Data.IDataReader dr;
                        dr = db.DataReader(strSql);
                        if (dr.Read())
                        {
                            rowCun = SysFun.ToInt(dr["cun"]);
                        }
                        dr.Close();
                        dr.Dispose();

                        if (rowCun > 0)
                        {
                            Response.Write("<script language=JavaScript>alert('该企业在商品库中有商品存在或已经投标，不能删除！！！');</script>");
                        }
                        else
                        {
                            strSql = "delete from Account where AccountId in (" + delAccountId + ")";
                            db.Command(strSql);
                            strSql = "delete from Company where AccountId in (" + delAccountId + ")";
                            db.Command(strSql);
                        }
                        db.Close();
                    }
                    else
                    {
                        Response.Write("<script language=JavaScript>alert('您不是管理员，所以不能删除！！！，如确定要删除请于管理员联系！！！');</script>");
                    }
                }
            }

            showGridView();
        }

    }
    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "v_company";                                               //要查询的表明
        dbPage.Key = "accountid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "case IsSendin when 1 then '已递交' else '未递交' end as IsSendin,usercode_xt,accountid,CompanyName,UserCode,case when  CheckStatus is null then '未审核' when  CheckStatus='0' then '未审核' when  CheckStatus='1' then '审核通过' when  CheckStatus='2'then '审核不通过' when  CheckStatus='3'then '资料一致' end CheckStatus, AccountName_SHR ,AccountName_TJR ,memo6,checktime,sort,pnd";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 10;                                                    //没有显示记录条数   
        dbPage.OrderBy = "UserCode desc,CompanyName";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键




    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "1=1 and", CompanyName = "", _UserCode, QYType, CheckStatus, _AccountName_SHR, Memo2;
        QYType = lstQYType.SelectedValue.ToString();
        switch (QYType)
        {
            case "全部":
                break;
            case "经营企业":
                strtemp = " Sort=1 and";
                break;
            case "生产企业":
                strtemp = " Sort=0 and";
                break;
            case "一级代理商":
                strtemp = " Sort=2 and";
                break;
        }
        _AccountName_SHR = lstSHR.SelectedValue;

        Memo2 = lstMemo2.SelectedValue;
        switch (Memo2)
        {
            case "全部":
                break;
            case "已保存未提交":
                strtemp = strtemp + " memo2=0 and";
                break;
            case "已提交":
                strtemp = strtemp + " memo2=1 and";
                break;
            case "已修改":
                strtemp = strtemp + " memo2=2 and";
                break;
        }
        CheckStatus = lstCheckStatus.SelectedValue.ToString();
        switch (CheckStatus)
        {
            case "全部":
                break;
            case "未审核":
                strtemp = strtemp + " (CheckStatus=0 or CheckStatus is null) and";
                break;
            case "审核通过":
                strtemp = strtemp + " CheckStatus=1 and";
                break;
            case "审核未通过":
                strtemp = strtemp + " CheckStatus=2 and";
                break;
            case "资料一致":
                strtemp = strtemp + " CheckStatus=3 and";
                break;
        }
        if (!LIsXL.SelectedValue.Equals("4"))//是否遴选
        {
            strtemp = strtemp + " isnull(isLX,0) ='" + LIsXL.SelectedValue + "' and";
        }
        CompanyName = txtAttorneyName.Text.ToString().Trim();
        _UserCode = txtUserCode.Text.ToString().Trim();
        if (_AccountName_SHR != "全部")
        {
            strtemp = strtemp + " AccountName_SHR like '%" + _AccountName_SHR + "%' and";
        }
        if (_UserCode != "")
        {
            strtemp = strtemp + " UserCode like '%" + _UserCode + "%' and";
        }
        if (CompanyName != "")
        {
            strtemp = strtemp + " (CompanyName like '%" + CompanyName + "%' or QYPYM like '%" + CompanyName + "%') and";
        }
        //是否递交
        switch (LIsSendin.SelectedIndex)
        {
            case 0:// 未递交 
                strtemp += " isnull(IsSendin,0)=0 and";
                break;
            case 1: //已递交
                strtemp += " isnull(IsSendin,0)=1 and";
                break;
            case 2:   //全部

                break;

        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    if (e.Row.Cells[3].Text == "审核不通过")
        //    {
        //        e.Row.BackColor = Color.Yellow;
        //    }
        //    e.Row.Cells[11].Text = " <input name=\"chk\" type=\"checkbox\" value='" + e.Row.Cells[0].Text + "'>";
        //}
        e.Row.Cells[0].Visible = false;
    }
    protected void ToEcxul_Click(object sender, EventArgs e)
    {

        Db db = new Db();
        System.Data.IDataReader dr;
        string _strSql = "Select CompanyName as 企业名 ,UserCode as 企业登录名 ,case when  CheckStatus is null then '未审核' when  CheckStatus='0' then '未审核' when  CheckStatus='1' then '审核通过' when  CheckStatus='2'then '审核不通过'  when  CheckStatus='3'then '资料一致'  end  as 审核状态 , AccountName_SHR as 审核人,AccountName_TJR as 添加人 ,checktime as 审核时间 ,ISNULL(memo5,'') AS 联系人,ISNULL(Telepone,'') AS 电话 ,ISNULL(memo10,'') AS 手机 From  v_company Where " + fctConditions();
        dr = db.DataReader(_strSql);
        GridView2.DataSource = dr;
        GridView2.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        string filename = System.Web.HttpUtility.UrlEncode("企业审核信息[" + txtShenheDate.Text.ToString() + lstCheckStatus.SelectedValue + "].xls");
        SysFun.WriteToExcel(GridView2, this.Page, filename);

    }

    protected void ToEcxul_Click2(object sender, EventArgs e)
    {

        Db db = new Db();
        System.Data.IDataReader dr;
        string _strSql = @"select usercode as 登录号 ,areaname as 所属地区, companyname as 企业名称,Lawer as 企业法人,IDcard as 企业法人身份证号, warrant as 生产许可证 , warrantdate as 许可证到期时间,businessscope as 经营范围,
TotalFund as 总资产, Fixfund as 固定资产,fund as [企业注册资产(万元)],lastincome as [上年营业额(万元)], 
registAddress as 注册地址 ,memo5 as 企业联系人,address as 联系地址,postcode as 邮编,telepone as 联系电话 ,memo10 as 手机,Fax as 传真,sos as 紧急联系电话,NetAddress as 网址,Email AS Email ,
codeid as 企业代码证号,codedate as 企业代码到期时间,Permission as 营业执照 ,case permissiondate when '1990-01-01 00:00:00.000' then '' else  permissiondate end as  营业执照到期时间,
case isLC  when '0' then'无' else '有' end as 是否有信用等级,case Lclevel when 1 then'A' when'2'then'AA'  when '3' then'AAA' else '' end as 信用等级,LcPermission as 信用证有效期,Introduction as 公司介绍, memo1 as 备注 from v_Company where sort=0 and areaid<>910000 and checkstatus=1";
        dr = db.DataReader(_strSql);
        GridView2.DataSource = dr;
        GridView2.DataBind();
        db.Close();
        dr.Close();
        dr.Dispose();
        string filename = System.Web.HttpUtility.UrlEncode("所有审核通过的生产企业.xls");
        SysFun.WriteToExcel(GridView2, this.Page, filename);

    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        //OverRide　为了使导出成Excel可行！

    }

    #region IPageDescribe 成员

    public string Name
    {
        get { return "企业管理"; }
    }

    public string Describe
    {
        get { return ""; }
    }

    public string ParentName
    {
        get { return "用户管理"; }
    }

    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAdmin);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCheckCompany);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumComapanyRG);
            return _AccountType;
        }
    }

    #endregion

    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)//解决科学计数法的问题
    {//1）  文本：vnd.ms-excel.numberformat:@
        //2）  日期：vnd.ms-excel.numberformat:yyyy/mm/dd
        //3）  数字：vnd.ms-excel.numberformat:#,##0.00
        //4）  货币：vnd.ms-excel.numberformat:￥#,##0.00
        //5）  百分比：vnd.ms-excel.numberformat: #0.00%
        for (int i = 0; i < e.Row.Cells.Count - 2; i++)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
                e.Row.Cells[i].Attributes.Add("style", "vnd.ms-excel.numberformat:@");
        }

    }
    // 确认递交
    protected void Button6_Click(object sender, EventArgs e)
    {
        Updateissendin(1);
    }
    //取消递交
    protected void Button7_Click(object sender, EventArgs e)
    {
        Updateissendin(0);
    }

    private void Updateissendin(int _issendin)
    {
        string _straccountidqy = "";
        if (Request.Form["chk"] != null && Request.Form["chk"].ToString() != "")
        {
            _straccountidqy = Request.Form["chk"].ToString();
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];

            if (HZ.MBSM.BLL.Company.UpdateIsSendid(_straccountidqy, _Account.AccountId.ToString(), _issendin))
            {
                Response.Write("<script>alert('提交成功！！！');</script>");
            }
            else
            {
                Response.Write("<script>alert('提交失败！！！');</script>");
            }
        }
        else
        {
            Response.Write("<script>alert('请选择企业！！！');</script>");
        }
        showGridView();
    }
}
